<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="js/jquery-1.12.4.min.js"></script>

</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            用户信息修改
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label ">用户名称：</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control username" id="inputEmail3" name="username" value="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">昵称：</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control nickname" name="nickname"  value="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">邮箱：</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control email" name="email" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">用户图标：</label>
                    <div class="col-sm-10">
                        <img src="images/2.jpg" alt="" class="user_pic">
                        <input type="file" id="exampleInputFile" name="user_pic">
                    </div>
                </div>

                <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">密码：</label>
                        <div class="col-sm-4">
                            <input type="password" class="form-control password"  value="password">
                        </div>
                    </div>


                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

</body>

</html>


<script>
    /*
    获取用户详细信息
    http://localhost:8000/admin/userinfo_get
    步骤:
    1.进入个人中心后
    2.调用获取详细信息接口
    3.获取到数据之后渲染到页面上


    修改用户信息
    http://localhost:8000/admin/userinfo_edit
    步骤:
    1.修改按钮点击事件
    2.获取表单数据
    3.ajax 携带着 formdata
    4.返回数据 提示用户


    本地图片预览
    1. input  file 绑定change事件
    2. 获取 this.files[0]
    3.调用方法生成url
    4.设置给img
     */

    $(function () {
        $.ajax({
          url:' http://localhost:8000/admin/userinfo_get',
          success:function (backData) {
            console.log(backData)
            for(const key in backData.data){
                $('input.' + key).val(backData.data[key])
            }

            $('.user_pic').attr('src',backData.data.user_pic)
          }
        })

      //change
      $('#exampleInputFile').change(function () {
        var file = this.files[0]
        var url = URL.createObjectURL(file);
        console.log(url,file)
        $(this).siblings('img').attr('src',url)
      })

      //保存
      $('.btn-success').click(function (e) {
        e.preventDefault()
        var formData = new FormData($('form')[0])

        $.ajax({
          url:'http://localhost:8000/admin/userinfo_edit',
          type:'post',
          data:formData,
          //不设置头部
          contentType:false,
          //不把数据变字符串
          processData:false,
          success:function (backData) {
            console.log(backData)
          }
        })
      })
    })
</script>












